package com.company.thread;

import java.util.concurrent.TimeUnit;

/**
 * @author jrue
 * @description 线程交替运行
 * @date 2019/12/4 14:36
 */
public class ThreadAlternate {

    public static void main(String[] args) {
        Thread thread1 = new Thread(new MyThread(),"thread01");
        Thread thread2 = new Thread(new MyThread(),"thread02");

        thread1.start();
        thread2.start();

    }


}
class MyThread implements Runnable {

    private Object lock = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName());
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}